package com.example.java_second_hand_market;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.*;
import java.util.List;

public class HistoryDialog extends Stage {
    VBox vbox;
    String DBUrl=ParameterDB.ParaDBUserUrl;
    String DBUser="root", DBPassword="123456";
    private final int ROWS_PER_PAGE = 10; // 每页显示的行数
    @FXML
    private TableView<Item> tableView = new TableView<>();
    @FXML
    private Pagination pagination;
    private ObservableList<Item> data=FXCollections.observableArrayList();

    Buyer by;
    public HistoryDialog(Buyer nby,int width,int height){
        by=nby;
        data = getInitialData();

        // 配置TableView
        setupTableView();

        // 创建分页控件
        pagination = new Pagination((data.size() + ROWS_PER_PAGE - 1) / ROWS_PER_PAGE);
        pagination.setPageFactory(this::createPage);

        vbox = new VBox(tableView, pagination);
        Scene scene = new Scene(vbox);
        this.setScene(scene);


        setTitle("历史记录");

    }
    public ObservableList<Item> getInitialData() {
        try{
            Connection  con= DriverManager.getConnection(DBUrl,DBUser,DBPassword);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("SELECT * FROM item_list WHERE buyer = '"+by.getName()+"'");
            while(rs.next()){
                data.add(new Item(rs.getString("name"),rs.getString("price"),rs.getString("seller"),rs.getInt("id")));
            }
            if(con!=null) {
                try {
                    rs.close();
                    stmt.close();
                    con.close();

                }catch (SQLException err){
                    System.out.println("11212");
                }
            }
        }catch (SQLException e){
            System.out.println("暂无该用户数据");
        }
        return data;
    }
    private void setupTableView() {
        TableColumn<Item, String> nameColumn = new TableColumn<>("姓名");
        nameColumn.setCellValueFactory(celldata ->{
            return new SimpleStringProperty(celldata.getValue().getName());
        });

        TableColumn<Item, String> priceColumn = new TableColumn<>("价格");
        priceColumn.setCellValueFactory(celldata ->{
            return new SimpleStringProperty(celldata.getValue().getPrice());
        });


        TableColumn<Item,String> sellerColumn = new TableColumn<>("商家");
        sellerColumn.setCellValueFactory(celldata ->{
            return new SimpleStringProperty(celldata.getValue().getSeller().getName());
        });
        tableView.getColumns().addAll(nameColumn, priceColumn,sellerColumn);
    }
    private VBox createPage(int pageIndex) {
        int fromIndex = pageIndex * ROWS_PER_PAGE;
        int toIndex = Math.min(fromIndex + ROWS_PER_PAGE, data.size());

        List<Item> pageItems = data.subList(fromIndex, toIndex);
        ObservableList<Item> pageData = FXCollections.observableArrayList(pageItems);
        tableView.setItems(pageData);

        return new VBox(tableView);
    }
    public  void refresh(){
        data.clear();
        data = getInitialData();

        // 配置TableView


        // 创建分页控件
        pagination .setPageCount((data.size() + ROWS_PER_PAGE - 1) / ROWS_PER_PAGE);
        pagination.setPageFactory(this::createPage);

        tableView.setItems(data); // 直接设置TableView的数据源
        tableView.refresh();
        vbox.getChildren().clear();
        vbox.getChildren().addAll(tableView,pagination);
    }
    public VBox  getVbox() {
        return vbox;
    }
}

